package com.zhr.listener;

import java.util.Set;
import java.util.TreeSet;

import com.zhr.handler.MessageHandler;
import com.zhr.server.WeixinRequest;
import com.zhr.server.WeixinResponse;

public abstract class MessageAbstractListener implements MessageListener {

	protected Set<MessageHandler> messageHandlers;

	public MessageAbstractListener() {
		messageHandlers = new TreeSet<MessageHandler>();
	}

	@Override
	public WeixinResponse execute(WeixinRequest request) {
		for (MessageHandler messageHandler : messageHandlers) {
			if (messageHandler.accpet(request)) {
				return messageHandler.handle(request);
			}
		}
		return null;
	}

	protected void registHandler(MessageHandler messageHandler) {
		messageHandlers.add(messageHandler);
	}

	protected void registHandlers(MessageHandler... messageHandlers) {
		for (MessageHandler messageHandler : messageHandlers) {
			this.messageHandlers.add(messageHandler);
		}
	}
}
